package parserXML
{
	import application.Configuration;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;

	/**
	 * Classe permettant de charger les fichiers de configurations
	 * et d'envoyer un évènement une fois les fichiers chargés
	 * @see XmlObject
	 */
	public class XmlConf extends EventDispatcher
	{
		/**
		 * Evenement de la classe envoye lorsque les différents fichiers xml de configuration sont charges
		 * @default COMPLETE_CHARGEMENT
		 */
		public static var COMPLETE_CHARGEMENT:String="COMPLETE_CHARGEMENT";

		/**
		 * Chemin relatif du fichier XML de configuration des rooms
		 * @default Architecture/structure/rooms/conf.xml
		 */
		private  var PATH_CONF_ROOM:String=Configuration.PATH_CONF_ROOM;
		/**
		 * Chemin relatif du fichier XML de configuration des rooms link
		 * @default Architecture/structure/rooms_link/conf.xml
		 */
		private  var PATH_CONF_LINKS_ROOM:String=Configuration.PATH_CONF_LINKS_ROOM;
		/**
		 * Chemin relatif du fichier XML de configuration des links link
		 * @default Architecture/structure/links_link/conf.xml
		 */
		private  var PATH_LINKS_LINK:String=Configuration.PATH_LINKS_LINK;

		private  var _CONF_ROOM:Dictionary=new Dictionary;
		private  var _CONF_LINKS_ROOM:Dictionary=new Dictionary;
		private  var _CONF_LINKS_LINK:Dictionary=new Dictionary;
		private  var _CONF_LINKS_LINK_BY_ID_LINK:Dictionary=new Dictionary;
		private  var _CONF_LINKS_ROOM_BY_ID_LINK:Dictionary=new Dictionary;

		/*Loader des fichiers XML de configuration*/
		private var xmlLoaderRoom:XmlLoader;
		private var xmlLoaderLinksRoom:XmlLoader;
		private var xmlLoaderLinksLink:XmlLoader;

		/*Nombre de fichier XML de configurations à charges*/
		private var cptChargement:int=3;

		/**
		 * Constructeur
		 */
		public function XmlConf()
		{
		}

		/**
		 * Demarre le chargement des fichiers XML de configuration
		 */
		public function chargementConf():void
		{
			xmlLoaderRoom=new XmlLoader(PATH_CONF_ROOM);
			xmlLoaderLinksRoom=new XmlLoader(PATH_CONF_LINKS_ROOM);
			xmlLoaderLinksLink=new XmlLoader(PATH_LINKS_LINK);
			xmlLoaderRoom.chargeur.addEventListener(Event.COMPLETE, chargementConfRoom);
			xmlLoaderLinksRoom.chargeur.addEventListener(Event.COMPLETE, chargementConfLinksRoom);
			xmlLoaderLinksLink.chargeur.addEventListener(Event.COMPLETE, chargementConfLinksLink);

		}


		/**
		 * Chargement du fichier de configuration des rooms
		 * @param e Event.COMPLETE
		 */
		private function chargementConfRoom(e:Event):void
		{
			var xml:XML=new XML(xmlLoaderRoom.chargeur.data);

			var configuration:XMLList=xml.index.room;
			var i:int=0;
			for (i=0; i < configuration.length(); i++)
			{
				var room:Array=new Array;
				room[0]=configuration[i].@id.toString();
				room[1]=configuration[i].@name.toString();
				room[2]=configuration[i].@path.toString();
				_CONF_ROOM[configuration[i].@id.toString()]=room;
			}
			
			cptChargement-=1;
			testComplete();
		}

		/**
		 * Chargement du fichier de configuration des links_link
		 * @param e Event.COMPLETE
		 */
		private function chargementConfLinksLink(e:Event):void
		{
			var xml:XML=new XML(xmlLoaderLinksLink.chargeur.data);
			var configuration:XMLList=xml.index.LinksLink;
			for (var i:int=0; i < configuration.length(); i++)
			{
				var linksLinkid:Object=new Object;
				linksLinkid[0]=configuration[i].@id.toString();
				linksLinkid[1]=configuration[i].@id_link.toString();
				linksLinkid[2]=configuration[i].@id_link_room.toString();
				linksLinkid[3]=configuration[i].@name.toString();
				linksLinkid[4]=configuration[i].@path.toString();
				_CONF_LINKS_LINK_BY_ID_LINK[configuration[i].@id_link.toString()]=linksLinkid;
				
								
				if (_CONF_LINKS_LINK[configuration[i].@id.toString()]!=null){
					var linksLink:Object=new Object;
					linksLink[0]=configuration[i].@id.toString();
					linksLink[1]=configuration[i].@id_link.toString();
					linksLink[2]=configuration[i].@id_link_room.toString();
					linksLink[3]=configuration[i].@name.toString();
					linksLink[4]=configuration[i].@path.toString();
					var vectTemp:Vector.<Object> = _CONF_LINKS_LINK[configuration[i].@id.toString()];
					vectTemp.push(linksLink);
					
					
				}else{
					var allLinksLink:Vector.<Object> = new Vector.<Object>;
					var linksLink:Object=new Object;
					linksLink[0]=configuration[i].@id.toString();
					linksLink[1]=configuration[i].@id_link.toString();
					linksLink[2]=configuration[i].@id_link_room.toString();
					linksLink[3]=configuration[i].@name.toString();
					linksLink[4]=configuration[i].@path.toString();
					allLinksLink.push(linksLink);
					_CONF_LINKS_LINK[configuration[i].@id.toString()]=allLinksLink;
				}
				
			}
			cptChargement-=1;
			testComplete();
		}

		/**
		 * Chargement du fichier de configuration des links room
		 * @param e Event.COMPLETE
		 */
		private function chargementConfLinksRoom(e:Event):void
		{

			var xml:XML=new XML(xmlLoaderLinksRoom.chargeur.data);
			var configuration:XMLList=xml.index.LinkRoom;
			
			for (var i:int=0; i < configuration.length(); i++)
			{
				var Linkroomid:Object=new Object;
				Linkroomid[0]=configuration[i].@id.toString();
				Linkroomid[1]=configuration[i].@name.toString();
				Linkroomid[2]=configuration[i].@id_link.toString();
				Linkroomid[3]=configuration[i].@path.toString();
				_CONF_LINKS_ROOM_BY_ID_LINK[configuration[i].@id_link.toString()]=Linkroomid;

				if (_CONF_LINKS_ROOM[configuration[i].@id.toString()]!=null){
					var Linkroom:Object=new Object;
					Linkroom[0]=configuration[i].@id.toString();
					Linkroom[1]=configuration[i].@name.toString();
					Linkroom[2]=configuration[i].@id_link.toString();
					Linkroom[3]=configuration[i].@path.toString();
					var vectTemp:Vector.<Object> = _CONF_LINKS_ROOM[configuration[i].@id.toString()];
					vectTemp.push(Linkroom);
					
				}else{
					var allLinkRoom:Vector.<Object> = new Vector.<Object>;
					var Linkroom:Object=new Object;
					Linkroom[0]=configuration[i].@id.toString();
					Linkroom[1]=configuration[i].@name.toString();
					Linkroom[2]=configuration[i].@id_link.toString();
					Linkroom[3]=configuration[i].@path.toString();
					allLinkRoom.push(Linkroom);
					_CONF_LINKS_ROOM[configuration[i].@id.toString()]=allLinkRoom;
				}
				
			}
			cptChargement-=1;
			testComplete();
		}

		/**
		 * Déclence un evenement COMPLETE_CHARGEMENT lorsque la variable
		 * cptChargement est égal à 0
		 */
		private function testComplete():void
		{
			if (cptChargement <= 0)
			{
				this.dispatchEvent(new Event("COMPLETE_CHARGEMENT"));
			}
		}
		
		/**
		 * Dictionnaire contenant les informations parser du fichier XML de configuration des rooms
		 * @return Dictionary
		 * @see Dictionary
		 */
		public function get CONF_ROOM():Dictionary
		{
			return this._CONF_ROOM;
		}
		
		/**
		 * Dictionnaire contenant les informations parser du fichier XML de configuration des rooms link
		 * @return Dictionary
		 * @see Dictionary
		 */
		public function get CONF_LINKS_ROOM():Dictionary
		{
			return this._CONF_LINKS_ROOM;
		}
		
		/**
		 * Dictionnaire contenant les informations parser du fichier XML de configuration des links link
		 * @return Dictionary
		 * @see Dictionary
		 */
		public function get CONF_LINKS_LINK():Dictionary
		{
			return this._CONF_LINKS_LINK;
		}
		
		/**
		 * Dictionnaire contenant les informations parser du fichier XML de configuration des links link par id_link
		 * @return Dictionary
		 * @see Dictionary
		 */
		public function get CONF_LINKS_LINK_BY_ID_LINK():Dictionary
		{
			return this._CONF_LINKS_LINK_BY_ID_LINK;
		}
		
		/**
		 * Dictionnaire contenant les informations parser du fichier XML de configuration des links link
		 * @return Dictionary
		 * @see Dictionary
		 */
		public function get CONF_LINKS_ROOM_BY_ID_LINK():Dictionary
		{
			return this._CONF_LINKS_ROOM_BY_ID_LINK;
		}
	}
}